package JavaSE.d5;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/*
设计一个员工类Employee
设计一个员工类Employee，具有员工编号、姓名、性别、住址、出生日期属性，覆盖Object的equals和toString方法。
在测试类中，分别创建两个员工对象，比较是否相等，并打印员工信息。
将字符串“”中的信息抽取出来，封装到创建的员工对象中。
 */
public class Test3 {
    public static void main(String[] args) throws ParseException {
        String s="12-007,张三,男,上海浦东新区15号,1983-2-6";
        String[] split = s.split(",");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d");
        Date parse = simpleDateFormat.parse(split[split.length - 1]);
        Employee employee = new Employee(split[0], split[1], split[2], split[3], parse);
        Employee employee2 = new Employee(split[0], split[1], split[2], split[3], parse);
        Employee employee3 = new Employee("12-008", "李四", split[2], split[3], parse);

        System.out.println(employee);
        System.out.println(employee.equals(employee2));
        System.out.println(employee.equals(employee3));
    }
}

class Employee {
    String EmployeeId;
    String name;
    String gender;
    String address;
    Date birth;

    public Employee(String employeeId, String name, String gender, String address, Date birth) {
        this.EmployeeId = employeeId;
        this.name = name;
        this.gender = gender;
        this.address = address;
        this.birth = birth;
    }

    @Override
    public String toString() {
        String birth=new SimpleDateFormat("yyyy-M-d").format(this.birth);
        return "Employee{" +
                "EmployeeId=" + EmployeeId +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", address='" + address + '\'' +
                ", birth=" + birth +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(EmployeeId, employee.EmployeeId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(EmployeeId, name, gender, address, birth);
    }
}